import axios from 'axios'
import util from '@/util/util.js'

const service = axios.create({
  baseURL: 'http://webapi1.ypt56.net/api/',
  // baseURL: 'http://www.ypt56.net:8088/api/',
  timeout: 15000
})

service.interceptors.request.use(config => {
  // config.headers = {
  //   'Content-Type': 'multipart/form-data'
  // }
  if (config.url !== 'LoginValidate') {
    const user = util.getItem('$user')
    if (user) {
      const token = {
        rtk: user.rtk,
        uid: user.uid,
        un: user.un
      }
      Object.assign(config.data, token)
    } else {
      // $vue.$router.push({path: '/login'})
    }
  }

  return config
}, error => {
  console.error(error) // for debug
  Promise.reject(error)
})

service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('err' + error) // for debug
  }
)

export default service
